package com.hyfd.task;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.hyfd.common.utils.GetUtil;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.ToolHttp;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class HuaGuFlowTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	OrderDao orderDao;//订单
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	private static Logger log = Logger.getLogger(BoLuoMiNewFlowTask.class);
	
//	@Scheduled(fixedDelay = 60000)
	public void queryBoLuoMiNewOrder(){
		Map<String,Object> map = new HashMap<String,Object>();

		int flag = -1;
		try{
			String id = "0000000101";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);//获取通道的数据
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			String queryOrderUrl = channel.get("link_url")+"";//查询地址
			String token = paramMap.get("token");
			String appSecret = paramMap.get("appSecret");
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", id);
			param.put("status", "1");
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			for(Map<String,Object> order : orderList){
				String orderId = order.get("orderId")+"";
				map.put("orderId", orderId);
				String order_num = order.get("providerOrderId")+"";
				String params = appSecret+"order_num="+order_num+"&token="+token+appSecret;
	            String sign = string2MD5(params);
	            JsonObject data = new JsonObject();
	    		data.addProperty("token", token);
	    		data.addProperty("order_num", order_num);
	    		data.addProperty("sign", sign);// 设置签名
	    		String par =  "order_num=" + order_num + "&token="+ token + "&sign=" + sign;
	    		String contentType = "application/x-www-form-urlencoded";
	    		String postUrl = queryOrderUrl+"/query";
	    		String retMsg = ToolHttp.post(false, postUrl, par, contentType);
				if(null!=retMsg&&!"".equals(retMsg)){
					JSONObject jObject = JSONObject.parseObject(retMsg);
	                String resultCode = jObject.getString("code");
	                String resultMsg = jObject.getString("msg");
	                String result = jObject.getString("result");
	                if ("0".equals(resultCode))
	                {
	                	JSONObject resultObj = JSONObject.parseObject(result);
	                	String status = resultObj.getString("status");
	                	String results = resultObj.getString("result");
	                	map.put("resultCode", status + ":" + results);
	                	if("0".equals(status)||"1".equals(status)){
	                		continue;
	                	}else if("2".equals(status)){
	                		flag=1;
	                	}else if("4".equals(status)){
	                		flag=0;
	                	}
	                }else{
	                	continue;
	                }
				}else{
					continue;
				}
				map.put("status", flag);
				mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
			}
		}catch(Exception e){
			log.error("huagu流量查询Task出错"+e);
		}
	}
	
	 /***       
     * MD5加码 生成32位md5码    
    */       
    public String string2MD5(String inStr){           
    	MessageDigest md5 = null;           
    	try{               
    		md5 = MessageDigest.getInstance("MD5");          
    	}catch (Exception e){               
    			e.printStackTrace();               
    			return "";           
    	}           
    	char[] charArray = inStr.toCharArray();     
    	byte[] byteArray = new byte[charArray.length];              
    	for (int i = 0; i < charArray.length; i++)               
    		byteArray[i] = (byte) charArray[i];           
    	byte[] md5Bytes = md5.digest(byteArray);           
    	StringBuffer hexValue = new StringBuffer();   
    	for (int i = 0; i < md5Bytes.length; i++){               
    		int val = ((int) md5Bytes[i]) & 0xff;               
    		if (val < 16)                   
    			hexValue.append("0");               
    			hexValue.append(Integer.toHexString(val));           
    		}           
    	return hexValue.toString();       
    } 

}
